import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

/**
 * Aux io device.
 * waits random time than unblocks passed process.
 * @author Patrick Black
 *
 */
public class OtherIO implements Runnable {
	CPU cpu;
	Process process;
	public OtherIO(CPU cpu, Process process) {
		this.cpu = cpu;
		this.process = process;
	}

	/**
	 * runs for a random amount of timer and then unblocks passed in process.
	 */
	@Override
	public void run() {
		int interval = 0 + (int)(Math.random()*100);
		
		Timer time = new Timer(interval, new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			cpu.unblock(process);
			System.out.println("System call for process " + process.fetchName() + " completed. " +
					"Process " + process.fetchName() + " no longer blocked.");
			System.out.println();
		}
		});
		
		time.setRepeats(false);
		time.start();
	}
}
